import React from 'react';
import { Button, Tabs, Modal } from 'antd';
import { inject, observer } from 'mobx-react';
import { WeaRightMenu, WeaReqTop, WeaNewScroll, WeaTools,WeaAlertPage } from "ecCom"
import { toJS } from "mobx"
import CptInfo from '../public/cptInfo'
import CptStatistics from '../public/cptStatistics'
import CptFlowLog from '../public/cptFlowLog'
import CptModify from '../public/cptModify'
import CptShare from "../public/cptShare"
import Cptmend from "../public/cptMend"
import MapDialog from "./MapDialog"
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptCardStore')
@observer
class CapitalCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        }
    }
    componentDidMount() {
        const { cptCardStore, location: { query } } = this.props;
        const { getCptTabs, getCptInfo } = cptCardStore;
        getCptTabs(query);
        getCptInfo({ viewtype: 'view', ...query });
    }
    componentWillReceiveProps(nextProps) {
        const keyOld = this.props.location.key;
        const keyNew = nextProps.location.key;
        //点击菜单路由刷新组件
        if (keyOld !== keyNew) {
            const { cptCardStore, location: { query } } = nextProps;
            const { getCptTabs, getCptInfo } = cptCardStore;
            getCptTabs(query);
            getCptInfo({ viewtype: 'view', ...query });
        }
    }

    render() {
        const { cptCardStore } = this.props;
        const { tabDatas, selectTabKey, loading, cptname, cptForm, cptInfo, staticCondition, staticForm, shareStore, shareTableStore, isShowShare, setValidate, hasRight, verified } = cptCardStore;
        if (verified && !hasRight) {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@543qle`} >
                <div style={{ color: '#000' }}>
                    {getLabel(2012, "对不起，您暂时没有权限！")}
                </div>
            </WeaAlertPage>
            )
        }
        if (verified && hasRight) {
            const tabDatas1 = toJS(tabDatas);
            let showScroll = true;
            let tabUrl = function (key) {
                if (tabDatas1) {
                    for (let i = 0; i < tabDatas1.length; i++) {
                        if (tabDatas1[i].key == key && tabDatas1[i].type == "cpt") {   //资产信息
                            return [<CptInfo ecId={`${this && this.props && this.props.ecId || ''}_CptInfo@3qfg1r`} store={cptCardStore} form={cptForm} condition={cptInfo} setValidate={setValidate} />, <Cptmend ecId={`${this && this.props && this.props.ecId || ''}_Cptmend@o0oefl`} type="card" contentStore={cptCardStore} />,<MapDialog ecId={`${this && this.props && this.props.ecId || ''}_MapDialog@t0hm3v`} store={cptCardStore}/>]
                        } else if (tabDatas1[i].key == key && tabDatas1[i].type == "statistics") {//统计报告
                            return <CptStatistics ecId={`${this && this.props && this.props.ecId || ''}_CptStatistics@29227t`} form={staticForm} condition={staticCondition} />
                        } else if (tabDatas1[i].key == key && tabDatas1[i].type == "flowlog") { //流转日志
                            showScroll = false;
                            return <CptFlowLog ecId={`${this && this.props && this.props.ecId || ''}_CptFlowLog@3oop7c`} contentStore={cptCardStore} />
                        } else if (tabDatas1[i].key == key && tabDatas1[i].type == "modify") {  //变更记录
                            showScroll = false;
                            return <CptModify ecId={`${this && this.props && this.props.ecId || ''}_CptModify@5955n8`} contentStore={cptCardStore} />
                        } else if (tabDatas1[i].key == key && tabDatas1[i].type == "shareinfo") {  //共享设置
                            showScroll = false;
                            return <CptShare ecId={`${this && this.props && this.props.ecId || ''}_CptShare@wpcqjt`} shareStore={shareStore} shareTableStore={shareTableStore} isShowShare={isShowShare} contentStore={cptCardStore} />
                        } else if (tabDatas1[i].key == key && tabDatas1[i].type == "") {
                            return tabDatas1[i].linkurl;
                        }
                    }
                    return ""
                }
            }(selectTabKey);
            return (
                <div>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@qaffnz`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}>
                        <WeaReqTop ecId={`${this && this.props && this.props.ecId || ''}_WeaReqTop@6gd2dt`}
                            title={cptInfo.title || ""}
                            loading={loading}
                            icon={<i className='icon-coms-fa' />}
                            iconBgcolor='#f14a2d'
                            buttons={this.getButtons()}
                            buttonSpace={10}
                            showDropIcon={true}
                            dropMenuDatas={this.getRightMenu()}
                            onDropMenuClick={this.onRightMenuClick.bind(this)}
                            tabDatas={tabDatas}
                            selectedKey={selectTabKey}
                            onChange={this.taskCardChangeTab.bind(this)}
                        >
                            <div className="cpt-req-content">
                                <div className='cpt-req-content-inner'>
                                    {showScroll ? <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@rje1t1`} scrollId='cpt-req-content-main-scroll' height='100%'>
                                            {
                                                typeof tabUrl === "object" ? tabUrl :
                                                    <iframe src={tabUrl} id="cpttabiframe" name="cpttabiframe" className="flowFrame" frameborder="0" width="100%" height="100%" />
                                            }</WeaNewScroll> : tabUrl
                                    }
                                </div>
                            </div>
                        </WeaReqTop>
                    </WeaRightMenu>
                </div>
            )
        }
        return (<div></div>)
    }
    getButtons() {
        const { cptCardStore } = this.props;
        const { selectTabKey, cptInfo, capitalid, shareRightMenu, shareTableStore } = cptCardStore;
        let rightMenu = [];
        if (selectTabKey == "1") {
            rightMenu = toJS(cptInfo.rightMenus) || [];
        }
        if (selectTabKey == "2") {
            rightMenu = toJS(shareRightMenu) || [];
        }
        let btnArr = [];
        let that = this;

        let shareDelteDisabeld = false;
        let { selectedRowKeys } = shareTableStore;
        if (selectedRowKeys.length == 0) {
            shareDelteDisabeld = true;
        }

        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fbo4iv@${m.type}`} type="primary"
                    disabled={m.type == 'BTN_DELETE_SHARE' ? shareDelteDisabeld : false}
                    onClick={() => {
                        let fn = m.menuFun.indexOf('this') >= 0 ? `${m.menuFun.split('this')[0]})` : m.menuFun;
                        if (selectTabKey == "1") {  //资产信息
                            if (m.type == "BTN_EDIT") { //编辑
                                cptCardStore.getCptInfo({ viewtype: 'edit', capitalid: capitalid });
                            } else if (m.type == "BTN_SUBMIT") {  //保存
                                cptCardStore.saveCptInfo({ capitalid: capitalid, isdata: 2 }, "card");
                            }
                        }
                        if (selectTabKey == "2") { //
                            if (m.type == "BTN_ADD_SHARE") { //
                                cptCardStore.showAddShareModal(true);
                            } else if (m.type == "BTN_DELETE_SHARE") {
                                cptCardStore.doDeteleShare({ capitalid: capitalid });
                            }
                        }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }
    getRightMenu() {
        const { cptCardStore } = this.props;
        const { selectTabKey, cptInfo, shareRightMenu, shareTableStore } = cptCardStore;
        let rightMenu = [];
        if (selectTabKey == "1") {
            rightMenu = toJS(cptInfo.rightMenus) || [];
        }
        if (selectTabKey == "2") {
            rightMenu = toJS(shareRightMenu) || [];
        }
        let btnArr = [];

        let shareDelteDisabeld = false;
        let { selectedRowKeys } = shareTableStore;
        if (selectedRowKeys.length == 0) {
            shareDelteDisabeld = true;
        }

        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            btnArr.push({
                disabled: m.type == 'BTN_DELETE_SHARE' ? shareDelteDisabeld : false,
                icon: <i className={m.menuIcon} />,
                content: m.menuName
            })
        });
        return btnArr
    }
    onRightMenuClick(key) {
        const { cptCardStore } = this.props;
        const { selectTabKey, cptInfo, capitalid, shareRightMenu } = cptCardStore;
        let rightMenu = [];
        let that = this;
        if (selectTabKey == "1") {
            rightMenu = toJS(cptInfo.rightMenus) || [];
        }
        if (selectTabKey == "2") {
            rightMenu = toJS(shareRightMenu) || [];
        }
        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            if (Number(key) == i) {
                if (selectTabKey == "1") {
                    if (m.type == "BTN_EDIT") { //编辑
                        cptCardStore.getCptInfo({ viewtype: 'edit', capitalid: capitalid });
                    } else if (m.type == "BTN_SUBMIT") {  //保存
                        cptCardStore.saveCptInfo({ capitalid: capitalid, isdata: 2 }, "card");
                    } else if (m.type == "BTN_DELETE") {  //删除
                        Modal.confirm({
                            title: getLabel(15172, '系统提示'),
                            content: getLabel(83600, '您确认要删除吗?'),
                            onOk() {
                                //cptCardStore.doDetelCpt();
                                cptCardStore.onCptDel({ capitalid: capitalid }, 'card', that.updateLeftTree);
                            },
                            onCancel() {
                                
                            },
                        });
                    } else if (m.type == "BTN_BACK") { //返回
                        cptCardStore.getCptInfo({ viewtype: 'view', capitalid: capitalid });
                    } else if (m.type == "BTN_MEND") { //送修
                        cptCardStore.doMend(true, { capitalid: capitalid }, "card");
                    } else if (m.type == "BTN_GIVEBACK") { //归还
                        let params = { viewtype: 'cptbackone', capitalid: capitalid }
                        Modal.confirm({
                            title: getLabel(15172, '系统提示'),
                            content: getLabel(83559, '您确认要归还吗?'),
                            onOk() {
                                cptCardStore.doGiveback(params);
                            },
                            onCancel() { },
                        });
                    }
                }
                if (selectTabKey == "2") {
                    if (m.type == "BTN_ADD_SHARE") { //
                        cptCardStore.showAddShareModal(true);
                    } else if (m.type == "BTN_DELETE_SHARE") {
                        cptCardStore.doDeteleShare({ capitalid: capitalid });
                    }
                }

            }
        });
    }
    taskCardChangeTab(key) {
        const { cptCardStore } = this.props;
        cptCardStore.changeTab(key);
    }

    updateLeftTree = () => {
        const { cptCardStore } = this.props;
        cptCardStore.initTreeDatas();
    }


}

export default CapitalCard;